<?php

class Application_Model_Reserva extends Zend_Db_Table_Abstract {

    protected $_name = 'reservas';
    protected $_primary = 'idreservas';
    private static $instance;
    protected $_schema = 'pub';

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_Reserva();
            return self::$instance;
        }
        return self::$instance;
    }

    public function insert($data) {
        if (isset($data['nome']) && isset($data['telefone']) && isset($data['mesa']) && isset($data['data']) && isset($data['hora'])){
            parent::insert($data);
        }
    }

    public function update(array $data, $where = '') {
        $where = 'idreservas = ' . $data['idreservas'];
        parent::update($data, $where);
    }

    public function getById($id) {
        return $this->fetchRow("idreservas = " . $id);
    }

    public function getAll(){
        $sql = 'select * from '.$this->_schema.'.'.$this->_name;
        return $this->getAdapter()->fetchAll($sql);
    }
    
    public function remove($id){
        $where = ' idreservas = ' .$id;
        $data['hora_saida']= Zend_Date::now()->get('H:mm');
        parent::update($data, $where);
    }
    
    public function removeByDataMesa($data,$mesa){
        $where = ' mesa = ' .$mesa . '  and  data=\''.$data.'\'';
        $dados['hora_saida']= Zend_Date::now()->get('H:mm');
        parent::update($dados, $where);
    }

    public function getByDateMesa($date,$mesa){
        $sql = "select * from reservas where data = '$date'
                and mesa = ".$mesa."
                and hora_saida is null";
        return $this->getAdapter()->fetchAll($sql);
    }
    
}